package com.jinlejia.erp.ui.mine.key;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.BaseActivity;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivityMyKeyDetailsBinding;
import com.jinlejia.erp.ui.house.HouseDetailActivity;
import com.jinlejia.erp.utils.DataFilter;
import com.zhy.http.okhttp.OkHttpUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import okhttp3.Call;


/**
 * 我的钥匙详情（Activity）
 */

public class MyKeyDetailsActivity extends BaseActivity {
    private ArrayList<String> sellinglist;
    private ArrayList<String> rentlist;
    private int type;
    private String state;
    private String rscode;
    private ActivityMyKeyDetailsBinding binding;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_my_key_details);
        binding.setEvent(this);

        switch (getIntent().getIntExtra("mold", 0)) {
            case 0:
                getSellingData();
                state = "shs";
                break;
            case 1:
                getRentData();
                state = "rent";
                break;
        }
        binding.keyHouseNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyKeyDetailsActivity.this, HouseDetailActivity.class);
                intent.putExtra("code", rscode);
                intent.putExtra("type", state);
                startActivity(intent);
            }
        });
    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.key_check_photo:
                Intent intent = new Intent(this, CheckPictureActivity.class);
                if (null != sellinglist && sellinglist.size() > 0) {
                    intent.putStringArrayListExtra("sellinglist", sellinglist);
                }
                if (null != rentlist && rentlist.size() > 0) {
                    intent.putStringArrayListExtra("rentlist", rentlist);
                }
                intent.putExtra("type", type);
                startActivity(intent);
                break;
        }
    }

    /**
     * 获取实勘买卖数据
     */
    private void getSellingData() {
        type = 0;
        OkHttpUtils
                .get()
                .addParams("applyCode", getIntent().getStringExtra("applyCode"))
                .url(ApiConfig.KEY_SELLING_DETAILS)
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(MyKeyDetailsActivity.this, response, null)) {
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            JSONObject data = JSON.parseObject(response.get("data").toString());
                            sellinglist = JSON.parseObject(data.getJSONArray("urlList").toString(), new TypeReference<ArrayList<String>>() {
                            });
                            Map<String, Object> list1 = JSON.parseObject(data.get("keyApply").toString(), new TypeReference<Map<String, Object>>() {
                            });
                            binding.keyHouseNumber.setText(list1.get("rscode").toString());
                            rscode = list1.get("rscode").toString();
                            if (list1.containsKey("estate") && !TextUtils.isEmpty(list1.get("estate").toString())) {
                                binding.keyCommunityName.setText(list1.get("estate").toString());
                            }
                            binding.keyDetailsTime.setText(sdf.format(new Date(Long.valueOf(list1.get("createTime").toString()))));
                            switch (list1.get("state").toString()) {
                                case "0":
                                    binding.keyState.setText("待审核");
                                    break;
                                case "1":
                                    binding.keyState.setText("审核通过");
                                    binding.keyAuditTime.setText(sdf.format(new Date(Long.valueOf(list1.get("checkTime").toString()))));
                                    binding.keyAuditPeople.setText(!TextUtils.isEmpty(list1.get("checkName").toString()) ? list1.get("checkName").toString() : "");
                                    binding.keyDismissReason.setText(!TextUtils.isEmpty(list1.get("reason").toString()) ? list1.get("reason").toString() : "");
                                    if (!TextUtils.isEmpty(list1.get("reason").toString())) {
                                        binding.reasonLayout.setVisibility(View.VISIBLE);
                                    }
                                    break;
                                case "2":
                                    binding.keyState.setText("驳回");
                                    binding.keyAuditTime.setText(sdf.format(new Date(Long.valueOf(list1.get("checkTime").toString()))));
                                    binding.keyAuditPeople.setText(!TextUtils.isEmpty(list1.get("checkName").toString()) ? list1.get("checkName").toString() : "");
                                    binding.keyDismissReason.setText(!TextUtils.isEmpty(list1.get("reason").toString()) ? list1.get("reason").toString() : "");
                                    if (!TextUtils.isEmpty(list1.get("reason").toString())) {
                                        binding.reasonLayout.setVisibility(View.VISIBLE);
                                    }
                                    break;
                            }

                        }
                    }
                });
    }

    /**
     * 获取实勘租赁数据
     */
    private void getRentData() {
        type = 1;
        OkHttpUtils
                .get()
                .addParams("applyCode", getIntent().getStringExtra("applyCode"))
                .url(ApiConfig.KEY_RENT_DETAILS)
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(MyKeyDetailsActivity.this, response, null)) {
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            JSONObject data = JSON.parseObject(response.get("data").toString());
                            rentlist = JSON.parseObject(data.getJSONArray("urlList").toString(), new TypeReference<ArrayList<String>>() {
                            });
                            Map<String, Object> list1 = JSON.parseObject(data.get("keyApply").toString(), new TypeReference<Map<String, Object>>() {
                            });
                            binding.keyHouseNumber.setText(list1.get("rscode").toString());
                            if (list1.containsKey("estate") && !TextUtils.isEmpty(list1.get("estate").toString())) {
                                binding.keyCommunityName.setText(list1.get("estate").toString());
                            }
                            binding.keyDetailsTime.setText(sdf.format(new Date(Long.valueOf(list1.get("createTime").toString()))));
                            switch (list1.get("state").toString()) {
                                case "0":
                                    binding.keyState.setText("待审核");
                                    break;
                                case "1":
                                    binding.keyState.setText("审核通过");
                                    binding.keyAuditTime.setText(sdf.format(new Date(Long.valueOf(list1.get("checkTime").toString()))));
                                    binding.keyAuditPeople.setText(!TextUtils.isEmpty(list1.get("checkName").toString()) ? list1.get("checkName").toString() : "");
                                    binding.keyDismissReason.setText(!TextUtils.isEmpty(list1.get("reason").toString()) ? list1.get("reason").toString() : "");
                                    if (!TextUtils.isEmpty(list1.get("reason").toString())) {
                                        binding.reasonLayout.setVisibility(View.VISIBLE);
                                    }
                                    break;
                                case "2":
                                    binding.keyState.setText("驳回");
                                    binding.keyAuditTime.setText(sdf.format(new Date(Long.valueOf(list1.get("checkTime").toString()))));
                                    binding.keyAuditPeople.setText(!TextUtils.isEmpty(list1.get("checkName").toString()) ? list1.get("checkName").toString() : "");
                                    binding.keyDismissReason.setText(!TextUtils.isEmpty(list1.get("reason").toString()) ? list1.get("reason").toString() : "");
                                    if (!TextUtils.isEmpty(list1.get("reason").toString())) {
                                        binding.reasonLayout.setVisibility(View.VISIBLE);
                                    }
                                    break;
                            }
                        }
                    }
                });
    }
}
